<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage, ElLoading } from 'element-plus'
import { adminLoginService } from '@/api/admin.js'
import { useTokenStore } from '@/stores/token.js'
import { useUserInfoStore } from '@/stores/userInfo.js'
import { useRouter } from 'vue-router'

// 路由和状态管理
const router = useRouter()
const tokenStore = useTokenStore()
const userInfoStore = useUserInfoStore()

// 表单状态
const isLogin = ref(true)
const loading = ref(false)
const remember = ref(false)
const passwordVisible = ref(false)

// 表单数据
interface LoginForm {
  username: string
  password: string
}

const form = ref<LoginForm>({
  username: localStorage.getItem('rememberedUsername') || 'admin',
  password: '12345'
})

// 表单规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 5, max: 20, message: '长度在 5 到 20 个字符', trigger: 'blur' }
  ]
}

// 提交表单
const submitForm = async () => {
  try {
    loading.value = true
    const result = await adminLoginService(form.value)
    
    ElMessage.success(result.msg || '登录成功')
    
    // 存储token和用户信息
    tokenStore.setToken(result.data.token)
    userInfoStore.setInfo(result.data.username)
    
    // 记住用户名
    if (remember.value) {
      localStorage.setItem('rememberedUsername', form.value.username)
    } else {
      localStorage.removeItem('rememberedUsername')
    }
    
    // 跳转到首页
    router.push('/')
  } catch (error) {
    ElMessage.error(error.response?.data?.msg || '登录失败，请重试')
  } finally {
    loading.value = false
  }
}

// 回车提交
const handleKeyUp = (e: KeyboardEvent) => {
  if (e.key === 'Enter') {
    submitForm()
  }
}
</script>

<template>
  <div class="login-container">
    <div class="login-bg">
      <img src="@/assets/background.png" alt="登录背景图" />
    </div>
    
    <div class="login-form">
      <div class="form-header">
        <h2 class="title">欢迎登录</h2>
        <p class="subtitle">请输入您的账号和密码</p>
      </div>
      
      <el-form 
        :model="form" 
        :rules="rules" 
        label-width="auto" 
        label-position="top"
        @keyup.enter="submitForm"
      >
        <el-form-item label="账号" prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名"
            clearable
            prefix-icon="User"
          />
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            :type="passwordVisible ? 'text' : 'password'"
            show-password
            prefix-icon="Lock"
            @keyup="handleKeyUp"
          />
        </el-form-item>
        
        <div class="form-actions">
          <el-checkbox v-model="remember">记住我</el-checkbox>
          <router-link to="/forgot-password" class="forgot-password">忘记密码?</router-link>
        </div>
        
        <el-form-item>
          <el-button 
            type="primary" 
            @click="submitForm" 
            :loading="loading"
            size="large"
            class="submit-btn"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped lang="scss">
.login-container {
  height: 100vh;
  display: flex;
  background-color: #fff;
  
  @media (max-width: 768px) {
    flex-direction: column;
  }
}

.login-bg {
  width: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f5f7fa;
  
  img {
    width: 70%;
    height: auto;
    max-height: 70%;
    object-fit: contain;
  }
  
  @media (max-width: 768px) {
    width: 100%;
    height: 30vh;
    
    img {
      width: 80%;
      height: auto;
    }
  }
}

.login-form {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 0 8%;
  max-width: 500px;
  margin: 0 auto;
  
  .form-header {
    margin-bottom: 40px;
    text-align: center;
    
    .title {
      font-size: 24px;
      margin-bottom: 10px;
      color: #333;
      font-weight: 500;
    }
    
    .subtitle {
      font-size: 14px;
      color: #999;
    }
  }
  
  .form-actions {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    
    .forgot-password {
      font-size: 14px;
      color: #999;
      text-decoration: none;
      
      &:hover {
        color: #409eff;
      }
    }
  }
  
  .submit-btn {
    width: 100%;
    margin-top: 10px;
  }
  
  .form-footer {
    text-align: center;
    margin-top: 20px;
    font-size: 14px;
    color: #666;
    
    .register-link {
      color: #409eff;
      text-decoration: none;
      margin-left: 5px;
      
      &:hover {
        text-decoration: underline;
      }
    }
  }
  
  @media (max-width: 768px) {
    padding: 30px 20px;
    margin-top: 20px;
    
    .form-header {
      margin-bottom: 30px;
    }
  }
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: #333;
}

:deep(.el-input__inner) {
  height: 48px;
  line-height: 48px;
}
</style>